package com.atguigu.day09;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink01_SQL_GroupWindow {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //3.创建表
        tableEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int, " +
                "t as to_timestamp(from_unixtime(ts/1000,'yyyy-MM-dd HH:mm:ss'))," +
                "watermark for t as t - interval '5' second)" +
                "with("
                + "'connector' = 'filesystem',"
                + "'path' = 'input/sensor-sql.txt',"
                + "'format' = 'csv'"
                + ")"
        );

        //TODO 4.开启一个滚动窗口
//        tableEnv.executeSql("select " +
//                "id," +
//                "sum(vc) sumVc," +
//                "tumble_start(t,interval '2' second) as wstart," +
//                "tumble_end(t,interval '2' second) as wend" +
//                " from sensor" +
//                " group by id,tumble(t,interval '2' second)")
//        .print();

        //开启一个滑动窗口
    /*    tableEnv.executeSql("select " +
                "id," +
                "sum(vc) sumVc," +
                "hop_start(t,interval '2' second,interval '3' second) as wstart," +
                "hop_end(t,interval '2' second,interval '3' second) as wend" +
                " from sensor" +
                " group by id,hop(t,interval '2' second,interval '3' second)").print();
*/

    //开启一个会话窗口
        tableEnv.executeSql("select " +
                "id," +
                "sum(vc) sumVc," +
                "session_start(t,interval '2' second) as wstart," +
                "session_end(t,interval '2' second) as wend" +
                " from sensor" +
                " group by id,session(t,interval '2' second)").print();
    }
}
